<?
if (file_exists('../stop.txt')){ //servizio interrotto
   header("location: stop.htm");
   exit();
}
require('../../pers/serverSSO.php');
if (!isset($aServerSSO[stripslashes($_GET['srv'])])) {
   $mess = "sisngle-sign-on fallito - server '".$_GET['srv']."'sconosciuto";
   header("location: login.php?mess=".urlencode($mess));
   exit();
}

/*///////////////////////////////////////////////

$host = $aServerSSO[stripslashes($_GET['srv'])]['host'];
$dir = $aServerSSO[stripslashes($_GET['srv'])]['dir'];

$fsock = fsockopen($host,80);
fputs($fsock, "GET $dir/".$_GET['rnd'].".php HTTP/1.0\r\n");
fputs($fsock, "Host: $host\r\n");
fputs($fsock, "Content-type: text/plain\r\n");
fputs($fsock, "Content-length: 0\r\n");
fputs($fsock, "Connection: close\r\n\r\n");
fputs($fsock, $dati);
$flag_body = false;
$buf = '';
while (!feof($fsock)) {
   $buf .= fgets($fsock,1024);
}
fclose($fsock);
$ax = explode("\tzxcv_",$buf);
if (count($ax) != 4) {
   $mess = "sisngle-sign-on fallito - verifica credenziali errata";
   header("location: login.php?mess=".urlencode($mess));
   exit();
}
if (urldecode($ax[1]) != $_GET['lgn']) {
   $mess = "sisngle-sign-on fallito - login errata";
   header("location: login.php?mess=".urlencode($mess));
   exit();
}
if (intval($ax[2]) > 5) {
   $mess = "sisngle-sign-on fallito - tempo scaduto";
   header("location: login.php?mess=".urlencode($mess));
   exit();
}
*////////////////////////////////////////////////////////////////

//riconosciuto utente autenticato windows
require_once('../../pers/inc.config.php');
if (file_exists(R2_FS_WEB_ROOT.'/stop.txt')) {
	header("location: ".R2_URL_ROOT."/app/_com/stop.php");
	exit();
}
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.set1.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/class.rint2Session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');

// configura il gestore dell'errore definito dall'utente
$oldErrorHandler = set_error_handler("erroHandler");

session_start();

$idUtente = 0;

foreach($aUtentiRint as $idUtX => $aUtX)  {
	if ($aUtX['login'] == strtolower(trim(reqVar('lgn')))) {
		$idUtente = $idUtX;
		$aUtente = $aUtX;			
		break;
	}
}
if ($idUtente === 0) {
   header("location: login.php?msgpag=".urlencode("Utente ".reqVar('lgn')." non censito"));
   exit();
}
$backDoor = false;

$msg = '';
$k=1;
while(isset($_SESSION[R2_SESS_OBJ_PREF.$k]) and $k < 100) {$k++;}
define('SESSOBJ',R2_SESS_OBJ_PREF.$k);
define('XSN',"$k.1");
$objSess = new rint2Session();
$objSess->PagSucc = 1;
$objSess->backDoor = $backDoor;
$objSess->login = strtolower(trim(reqVar('lgn')));
$objSess->nome = $aUtente['nome'];
$objSess->loginTime = time();
$objSess->urlIniz = "/app/rint/rintList.php";
$objSess->idUtente = $idUtente;
$objSess->mail = $aUtX['e_mail'];
$objSess->ric_ope = $aUtX['ric_ope'];
$objSess->abilitaz = $aUtX['abilitaz'];
// todo ??????????????????????????????????????????????????????????????????
$_SESSION[SESSOBJ] = $objSess;

rint2ScriviLog('login', "log-in ".$objSess->login);

header("Location: ".R2_URL_ROOT.$objSess->urlIniz."?xsn=".XSN);
exit();


function erroHandler($errno, $errstr, $errfile, $errline){
   switch ($errno) {
   case E_USER_WARNING:
   case E_USER_NOTICE:
   case E_WARNING:
   case E_NOTICE:
   case E_DEPRECATED:
   	echo "$errstr in $errfile alla linea $errline <br>";
      return;
   default:
      $uself = urlencode($_SERVER['PHP_SELF']);
      $uerrstr = urlencode($errstr);
      $uerrfile = urlencode($errfile);
      // aggiungere in querystring $errno $errstr $errfile $errline
      echo '<script>window.top.location.href = "'.R2_URL_ROOT.'/app/_com/error.php?'.
         'pagina_self='.$uself.'&errno='.$errno.'&errstr='.$uerrstr.
         '&errfile='.$uerrfile.'&errline='.$errline.'"</script>';
      exit;
      break;
   }
}
